package jtmcraft.game.units;

import java.awt.Color;
import java.io.Serializable;
import jtmcraft.game.CommandButton;
import jtmcraft.game.command.CommandNewCarrier;
import jtmcraft.game.Controller;
import jtmcraft.graphics.ImageResourceFactory;
import jtmcraft.graphics.Sprite;

/**
 *
 * @author milosz
 */
public class GameUnitFactory implements Serializable {

    private Controller actionHandler;

    public GameUnitFactory(Controller actionHandler) {
        this.actionHandler = actionHandler;
    }

    public enum GameUnitType
    {
        warrior,
        worker,
        worker_gold,
        barracs,
        factory,
        mine,
        storehouse,
        
    }

    public GameUnit createGameUnit( GameUnitType type ) {
        return createGameUnit(type, null, null);
    }
    
    public GameUnit createGameUnit( GameUnitType type, Color playerColor ) {
        return createGameUnit(type, null, playerColor);
    }

    public GameUnit createGameUnit( GameUnitType type, GameUnit home, Color playerColor ) {
        switch( type )
        {
            case warrior:
                return  createWarriorUnit( playerColor );
            case worker:
                return createWorkerUnit( playerColor, home );
            case worker_gold:
                return createWorkerGoldUnit( playerColor, home );
            case mine:
                return createMineUnit();
            case storehouse:
                return createStorehouseUnit(playerColor);
            default:
                throw new IllegalArgumentException( "Invalid game unit type: "
                        + type );
        }
    }

    private WorkerUnit createWorkerUnit( Color playerColor, GameUnit home )
    {
        WorkerUnit workerUnit = new WorkerUnit( home, actionHandler, playerColor );
        Sprite carrier = new Sprite();
    //    try {
        workerUnit.setSprite(carrier);

        carrier.process(System.nanoTime() / 1000000);
        carrier.setMove(false);

        carrier.AddSpriteImages(Sprite.eSpriteAction.moveN, ImageResourceFactory.eImageResource.eCarrierMoveN);

        carrier.AddSpriteImages(Sprite.eSpriteAction.moveNE, ImageResourceFactory.eImageResource.eCarrierMoveNE);
        carrier.AddSpriteImages(Sprite.eSpriteAction.moveE, ImageResourceFactory.eImageResource.eCarrierMoveE);
        carrier.AddSpriteImages(Sprite.eSpriteAction.moveSE, ImageResourceFactory.eImageResource.eCarrierMoveSE);
        carrier.AddSpriteImages(Sprite.eSpriteAction.moveS, ImageResourceFactory.eImageResource.eCarrierMoveS);
        carrier.AddSpriteImages(Sprite.eSpriteAction.moveSW, ImageResourceFactory.eImageResource.eCarrierMoveSW);
        carrier.AddSpriteImages(Sprite.eSpriteAction.moveW, ImageResourceFactory.eImageResource.eCarrierMoveW);
        carrier.AddSpriteImages(Sprite.eSpriteAction.moveWN, ImageResourceFactory.eImageResource.eCarrierMoveNW);

        Sprite carrierGold = new Sprite();
        workerUnit.setCarrierGoldSprite(carrierGold);
        carrierGold.process(System.nanoTime() / 1000000);
        carrierGold.setMove(false);

        carrierGold.AddSpriteImages(Sprite.eSpriteAction.moveN, ImageResourceFactory.eImageResource.eCarrierGoldMoveN);

        carrierGold.AddSpriteImages(Sprite.eSpriteAction.moveNE, ImageResourceFactory.eImageResource.eCarrierGoldMoveNE);
        carrierGold.AddSpriteImages(Sprite.eSpriteAction.moveE, ImageResourceFactory.eImageResource.eCarrierGoldMoveE);
        carrierGold.AddSpriteImages(Sprite.eSpriteAction.moveSE, ImageResourceFactory.eImageResource.eCarrierGoldMoveSE);
        carrierGold.AddSpriteImages(Sprite.eSpriteAction.moveS, ImageResourceFactory.eImageResource.eCarrierGoldMoveS);
        carrierGold.AddSpriteImages(Sprite.eSpriteAction.moveSW, ImageResourceFactory.eImageResource.eCarrierGoldMoveSW);
        carrierGold.AddSpriteImages(Sprite.eSpriteAction.moveW, ImageResourceFactory.eImageResource.eCarrierGoldMoveW);
        carrierGold.AddSpriteImages(Sprite.eSpriteAction.moveWN, ImageResourceFactory.eImageResource.eCarrierGoldMoveNW);

        Sprite.eSpriteAction aGold = Sprite.eSpriteAction.moveE; //Sprite.eSpriteAction.values()[n];
        carrierGold.setSpriteAction(aGold);


        Sprite.eSpriteAction a = Sprite.eSpriteAction.moveE; //Sprite.eSpriteAction.values()[n];
        carrier.setSpriteAction(a);

        ImageResourceFactory.eImageResource tmp = ImageResourceFactory.eImageResource.eCarrierMoveN;
        workerUnit.setWidth((int)(tmp.getActiveWidthHeight()[0] / (100 / tmp.getPreferredPercentSize()) ) );
        workerUnit.setHeight((int)(tmp.getActiveWidthHeight()[1] / (100 / tmp.getPreferredPercentSize()) ));

        return workerUnit;

    }

      private WorkerUnit createWorkerGoldUnit( Color playerColor, GameUnit home )
    {
        WorkerUnit workerUnit = new WorkerUnit( home, actionHandler, playerColor );
        Sprite carrier = new Sprite();
    //    try {
        workerUnit.setSprite(carrier);

        carrier.process(System.nanoTime() / 1000000);
        carrier.setMove(false);

        carrier.AddSpriteImages(Sprite.eSpriteAction.moveN, ImageResourceFactory.eImageResource.eCarrierGoldMoveN);

        carrier.AddSpriteImages(Sprite.eSpriteAction.moveNE, ImageResourceFactory.eImageResource.eCarrierGoldMoveNE);
        carrier.AddSpriteImages(Sprite.eSpriteAction.moveE, ImageResourceFactory.eImageResource.eCarrierGoldMoveE);
        carrier.AddSpriteImages(Sprite.eSpriteAction.moveSE, ImageResourceFactory.eImageResource.eCarrierGoldMoveSE);
        carrier.AddSpriteImages(Sprite.eSpriteAction.moveS, ImageResourceFactory.eImageResource.eCarrierGoldMoveS);
        carrier.AddSpriteImages(Sprite.eSpriteAction.moveSW, ImageResourceFactory.eImageResource.eCarrierGoldMoveSW);
        carrier.AddSpriteImages(Sprite.eSpriteAction.moveW, ImageResourceFactory.eImageResource.eCarrierGoldMoveW);
        carrier.AddSpriteImages(Sprite.eSpriteAction.moveWN, ImageResourceFactory.eImageResource.eCarrierGoldMoveNW);

        Sprite.eSpriteAction a = Sprite.eSpriteAction.moveE; //Sprite.eSpriteAction.values()[n];
        carrier.setSpriteAction(a);

        ImageResourceFactory.eImageResource tmp = ImageResourceFactory.eImageResource.eCarrierGoldMoveN;
        workerUnit.setWidth((int)(tmp.getActiveWidthHeight()[0] / (100 / tmp.getPreferredPercentSize()) ) );
        workerUnit.setHeight((int)(tmp.getActiveWidthHeight()[1] / (100 / tmp.getPreferredPercentSize()) ));

        return workerUnit;

    }


    private WarriorUnit createWarriorUnit( Color playerColor )
    {
        WarriorUnit warriorUnit = new WarriorUnit( actionHandler, playerColor );
        Sprite swordstanShield = new Sprite();
    //    try {
        warriorUnit.setSprite(swordstanShield);

        swordstanShield.process(System.nanoTime() / 1000000);
        swordstanShield.setMove(false);

        swordstanShield.AddSpriteImages(Sprite.eSpriteAction.moveN, ImageResourceFactory.eImageResource.eSwordstanShieldMoveN);

        swordstanShield.AddSpriteImages(Sprite.eSpriteAction.moveNE, ImageResourceFactory.eImageResource.eSwordstanShieldMoveNE);
        swordstanShield.AddSpriteImages(Sprite.eSpriteAction.moveE, ImageResourceFactory.eImageResource.eSwordstanShieldMoveE);
        swordstanShield.AddSpriteImages(Sprite.eSpriteAction.moveSE, ImageResourceFactory.eImageResource.eSwordstanShieldMoveSE);
        swordstanShield.AddSpriteImages(Sprite.eSpriteAction.moveS, ImageResourceFactory.eImageResource.eSwordstanShieldMoveS);
        swordstanShield.AddSpriteImages(Sprite.eSpriteAction.moveSW, ImageResourceFactory.eImageResource.eSwordstanShieldMoveSW);
        swordstanShield.AddSpriteImages(Sprite.eSpriteAction.moveW, ImageResourceFactory.eImageResource.eSwordstanShieldMoveW);
        swordstanShield.AddSpriteImages(Sprite.eSpriteAction.moveWN, ImageResourceFactory.eImageResource.eSwordstanShieldMoveNW);

        swordstanShield.AddSpriteImages(Sprite.eSpriteAction.attackN, ImageResourceFactory.eImageResource.eSwordstanShieldAttackN);
        swordstanShield.AddSpriteImages(Sprite.eSpriteAction.attackNE, ImageResourceFactory.eImageResource.eSwordstanShieldAttackNE);
        swordstanShield.AddSpriteImages(Sprite.eSpriteAction.attackE, ImageResourceFactory.eImageResource.eSwordstanShieldAttackE);
        swordstanShield.AddSpriteImages(Sprite.eSpriteAction.attackSE, ImageResourceFactory.eImageResource.eSwordstanShieldAttackSE);
        swordstanShield.AddSpriteImages(Sprite.eSpriteAction.attackS, ImageResourceFactory.eImageResource.eSwordstanShieldAttackS);
        swordstanShield.AddSpriteImages(Sprite.eSpriteAction.attackSW, ImageResourceFactory.eImageResource.eSwordstanShieldAttackSW);
        swordstanShield.AddSpriteImages(Sprite.eSpriteAction.attackW, ImageResourceFactory.eImageResource.eSwordstanShieldAttackW);
        swordstanShield.AddSpriteImages(Sprite.eSpriteAction.attackWN, ImageResourceFactory.eImageResource.eSwordstanShieldAttackNW);

        Sprite.eSpriteAction a = Sprite.eSpriteAction.moveE; //Sprite.eSpriteAction.values()[n];
        swordstanShield.setSpriteAction(a);

        ImageResourceFactory.eImageResource tmp = ImageResourceFactory.eImageResource.eSwordstanShieldMoveN;
        warriorUnit.setWidth((int)(tmp.getActiveWidthHeight()[0] / (100 / tmp.getPreferredPercentSize()) ) );
        warriorUnit.setHeight((int)(tmp.getActiveWidthHeight()[1] / (100 / tmp.getPreferredPercentSize()) ));
        
        return warriorUnit;

    }

    private MineUnit createMineUnit()
    {
        MineUnit mineUnit = new MineUnit( actionHandler );
        Sprite sprite = new Sprite();
        mineUnit.setSprite(sprite);

        sprite.AddSpriteImages( ImageResourceFactory.eImageResource.eBuildingMine);
        mineUnit.setWidth((int)(ImageResourceFactory.eImageResource.eBuildingMine.getActiveWidthHeight()[0] / (100 / ImageResourceFactory.eImageResource.eBuildingMine.getPreferredPercentSize()) ) );
        mineUnit.setHeight((int)(ImageResourceFactory.eImageResource.eBuildingMine.getActiveWidthHeight()[1] / (100 / ImageResourceFactory.eImageResource.eBuildingMine.getPreferredPercentSize()) ));

        return mineUnit;
    }

    private StorehouseUnit createStorehouseUnit( Color playerColor )
    {
        StorehouseUnit storehouseUnit = new StorehouseUnit( actionHandler, playerColor );
        Sprite sprite = new Sprite();
        storehouseUnit.setSprite(sprite);

        sprite.AddSpriteImages( ImageResourceFactory.eImageResource.eBuildingStorehouse);
        storehouseUnit.setWidth((int)(ImageResourceFactory.eImageResource.eBuildingStorehouse.getActiveWidthHeight()[0] / (100 / ImageResourceFactory.eImageResource.eBuildingStorehouse.getPreferredPercentSize()) ) );
        storehouseUnit.setHeight((int)(ImageResourceFactory.eImageResource.eBuildingStorehouse.getActiveWidthHeight()[1] / (100 / ImageResourceFactory.eImageResource.eBuildingStorehouse.getPreferredPercentSize()) ));

        CommandButton commandButton = new CommandButton( ImageResourceFactory.eImageResource.eNewCarrierNormal,
                ImageResourceFactory.eImageResource.eNewCarrierMouseOver,
                ImageResourceFactory.eImageResource.eNewCarrierPush );
        commandButton.setX(2);
        commandButton.setY(2);
        commandButton.addCommand( new CommandNewCarrier( actionHandler ));
        
        storehouseUnit.addCommandButton(commandButton);
        
        return storehouseUnit;
    }

}